'''
爬取巴乐兔房源
网址：https://sh.baletu.com/

难点：处理列表中的空格/r/n/t
'''

import requests
from lxml import etree

headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'priority': 'u=0, i',
    'referer': 'https://sh.baletu.com/zhaofang/p2o1a1/?seachId=0&is_rec_house=0&entrance=14&solr_house_cnt=10000',
    'sec-ch-ua': '"Chromium";v="130", "Microsoft Edge";v="130", "Not?A_Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0',
}

params = {
    'seachId': '0',
    'is_rec_house': '0',
    'entrance': '14',
    'solr_house_cnt': '10000',
}

response = requests.get('https://sh.baletu.com/zhaofang/p3o1a1/', params=params, headers=headers)
text = response.text
print(text)
parseHtml = etree.HTML(text)
## 获取房户名
titleList = parseHtml.xpath('//div[@class="list-center"]//div[@class="list-pic-title"]//a/text()')
## 获取户型信息
houseTypeList = parseHtml.xpath('//div[@class="list-center"]//p[@class="list-pic-ps"]/span/text()')
## 获取房源地址
houseAddressList = parseHtml.xpath('//div[@class="list-center"]//div[@class="list-pic-ad"]/text()')
## 获取房源价格
housePriceList = parseHtml.xpath('//div[@class="list-center"]//div[@class="price-num"]/span/text()')
## 循环处理 房户名的空格
_titleList = [x.strip() for x in titleList if x.strip()!='']
## 循环处理 房户型信息的空格
_houseTypeList = [x.strip() for x in houseTypeList if x.strip()!='']

for index in range(len(_titleList)):
    msg = '户名：%s，户型：%s，地址%s，价格：%s' % (_titleList[index], _houseTypeList[index], houseAddressList[index], housePriceList[index])
    print(msg)

